//
//  File.swift
//
//
//  Created by didi on 2022/9/13.
//

import MolecularObjects
import Vapor

extension Permission.List: Content { }
extension Permission.Detail: Content { }

struct SystemPermissionApiController: ApiController {
    typealias ApiModel = Permission
    typealias DatabaseModel = SystemPermissionModel
    
    typealias CreateContext = Permission.Create

    func listOutput(_ req: Request, _ models: [SystemPermissionModel]) async throws -> [Permission.List] {
        models.map {
            .init(id: $0.uuid, namespace: $0.namespace, context: $0.context, action: $0.action, name: $0.name)
        }
    }

    func detailOutput(_ req: Request, _ model: SystemPermissionModel) async throws -> Permission.Detail {
        .init(id: model.uuid, namespace: model.namespace, context: model.context, action: model.action, name: model.name)
    }

    func createInput(_ req: Request, _ model: SystemPermissionModel, _ input: Permission.Create) async throws {
        model.namespace = input.namespace
        model.context = input.context
        model.action = input.action
        model.name = input.name
        model.notes = input.notes
    }

    func updateInput(_ req: Request, _ model: SystemPermissionModel, _ input: Permission.Update) async throws {
        model.namespace = input.namespace
        model.context = input.context
        model.action = input.action
        model.name = input.name
        model.notes = input.notes
    }

    func patchInput(_ req: Request, _ model: SystemPermissionModel, _ input: Permission.Patch) async throws {
        model.namespace = input.namespace ?? model.namespace
        model.context = input.context ?? model.context
        model.action = input.action ?? model.action
        model.name = input.name ?? model.name
        model.notes = input.notes ?? model.notes
    }

    func validators(optional: Bool) -> [AsyncValidator] {
        []
    }
}
